package com.ganzi.ganziitem.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.Date;

@Service
public class RabbitReceiver {
    private static Logger log = LoggerFactory.getLogger(RabbitReceiver.class);

    @Autowired
    CollectionServer service;

    //指令类型
    public static final String QUERY__COLLECT = "-2";//查询采集器指令
    public static final String QUERY_SCHOOL_AND_COLLECT = "-1";//查询学校和采集器状态指令
    public static final String QUERY_ORTHER = "0";//查询其他指令
    public static final String CONTROL_ALL = "1";//控制全部指令

    //指令字段
    public static final String TYPE = "type";//指令类型字段
    public static final String SEND_COLLECT = "sendCollect";//采集器信息字段(数组)
    public static final String ALL_TYPE = "allType";//控制全部字段
    public static final String VALUE = "value";//指令值字段
    public static final String FAN_NUMBER = "fanNumber";//盘管编号字段
    public static final String IP = "ip";//ip字段
    public static final String ALL_COUNT = "allCount";//采集器中盘管总数字段


    @RabbitListener(queues ={"${rabbit.shoolQueue}"})
    public void process(Message message,Channel channel)throws IOException{
        try {
            log.info("服务器消息："+new String(message.getBody()));
            JSONObject info = (JSONObject) JSON.parse(message.getBody());
            String type = info.getString(TYPE);
            if(QUERY__COLLECT.equals(type)){//查询采集器在线状态
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);//回复rabbit.收到消息
                service.sendCollectionState();
            }else if(QUERY_SCHOOL_AND_COLLECT.equals(type)){//查询学校和采集器在线状态
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
                service.sendState();
            }else if(QUERY_ORTHER.equals(type)){//查询命令
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
                JSONArray sendCollect = info.getJSONArray(SEND_COLLECT);
                if(sendCollect != null)service.collection(sendCollect);
            }else{
                String allType = info.getString(ALL_TYPE);
                String value = info.getString(VALUE);
                if(allType != CONTROL_ALL){//控制单个
                    String fanNumber = info.getString(FAN_NUMBER);
                    String ip = info.getString(IP);
                    if(StringUtils.isBlank(ip)||StringUtils.isBlank(fanNumber)||StringUtils.isBlank(type)||StringUtils.isBlank(value)){
                        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
                        log.info("控制命令失效");
                    }else{
                        service.controll(type,value,fanNumber,ip);
                        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
                    }
                }else{//控制全部
                    if(StringUtils.isBlank(value)){
                        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
                        log.info("控制命令失效,控制类型不存在");
                    }else{
                        JSONArray sendCollect = info.getJSONArray(SEND_COLLECT);
                        if(sendCollect != null)service.controllAll(type,value,sendCollect);
                        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
                    }
                }
            }
        } catch (Exception e) {
            //回复rabbit.丢弃这条消息
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
            log.info("接收失败");
        }

    }
}
